﻿using Microsoft.Extensions.Logging;
using MockSchoolManagement.Infrastructure;
using MockSchoolManagement.Models;
using System.Collections.Generic;
using System.Linq;

namespace MockSchoolManagement.DataRepositories
{
    public class StudentRepository : IStudentRepository
    {
        private readonly AppDbContext _appDbContext;
        private readonly ILogger<StudentRepository> _logger;
        public StudentRepository(AppDbContext appDbContext,ILogger<StudentRepository> logger)
        {
            _appDbContext = appDbContext;
            _logger = logger;
        }
        public Student Add(Student student)
        {
            var info = _appDbContext.Students.Add(student);
            _appDbContext.SaveChanges();
            return student;
        }

        public Student Delete(int Id)
        {
            var info = _appDbContext.Students.Find(Id);
            if (info!=null)
            {
                _appDbContext.Students.Remove(info);
                _appDbContext.SaveChanges();

            }
            return info;
        }

        public IEnumerable<Student> GetAllStudents()
        {
            _logger.LogTrace("学生信息Trace(跟踪)Log");
            _logger.LogDebug("学生信息Debug(调试)Log");
            _logger.LogInformation("学生信息 信息(Information)Log");
            _logger.LogWarning("学生信息 警告(Warning)Log");
            _logger.LogError("学生信息 错误(Error)Log");
            _logger.LogCritical("学生信息 严重(Critical)Log");

            return _appDbContext.Students.ToList();
        }

        public Student GetStudent(int id)
        {
            return _appDbContext.Students.Find(keyValues: id);
        }

        public Student Update(Student student)
        {
            var info = _appDbContext.Students.FirstOrDefault(s => s.Id == student.Id);
            if (student != null)
            {
                info.Name = student.Name;
                info.Email = student.Email;
                info.Major = student.Major;
                info.PhotePath = student.PhotePath;
            }
            _appDbContext.SaveChanges();
            return info;
        }
    }
}
